package com.imessage.action.manage.role;

import com.bean.ManageResourceRole;
import com.bean.ManageRole;
import com.bean.ManageRoleUser;
import com.imessage.comm.Constants;
import com.imessage.vo.RoleVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/role")
public class RoleAction extends RoleBaseAction{


    /**
     * 列表，搜索数据
     */
    @RequestMapping(value = "")
    public Object run(RoleVo roleVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, roleVo);
        Page<ManageRole> page = baseService.findPage(ManageRole.class, expressionList,
                null, getOrder(roleVo.getOrders()), roleVo.getPageCurrent(),
                roleVo.getPageSize());
        return getRoleBeanPage(page);
    }


    /**
     * 添加修改页面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        StringBuffer manageMenuIdsb = new StringBuffer();
        ManageRole manageRole = new ManageRole();
        if (StringUtils.isNotBlank(id)) {
            manageRole = baseService.findById(ManageRole.class, id);
            List<ManageResourceRole> manageRoleMenuList = getManageRoleMenu(manageRole.getId());
            for (ManageResourceRole manageRoleMenu : manageRoleMenuList) {
                manageMenuIdsb.append(";");
                manageMenuIdsb.append(manageRoleMenu.getManageResourceId());
            }
            manageMenuIdsb.append(";");
            modelAndView.setViewName("admin/manage/role/modify");
        } else {
            modelAndView.setViewName("admin/manage/role/add");
        }
        setModelAndView(modelAndView, manageRole, manageMenuIdsb.toString());
        return modelAndView;
    }

    /**
     * 添加角色
     *
     * @param roleVo
     * @return
     */
    @RequestMapping(value = "add")
    public Object add(RoleVo roleVo) {
        HttpJson json = checkData(roleVo);
        if (json != null) {
            return json;
        }
        ManageRole manageRole = new ManageRole();
        setManageRole(roleVo, manageRole);
        int state = baseService.saveObject(manageRole);
        saveManageRoleMenu(roleVo.getMenuid(), manageRole.getId());
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 修改角色
     *
     * @param roleVo
     * @return
     */
    @RequestMapping(value = "modify")
    public Object modify(RoleVo roleVo) {
        HttpJson json = checkData(roleVo);
        if (json != null) {
            return json;
        }
        ManageRole manageRole = (ManageRole) baseService.findById(ManageRole.class, roleVo.getId());
        setManageRole(roleVo, manageRole);
        int state = baseService.updateObject(manageRole);
        List<ManageResourceRole> manageRoleMenuList = getManageRoleMenu(manageRole.getId());
        if (manageRoleMenuList != null && manageRoleMenuList.size() > 0) {
            for (ManageResourceRole manageRoleMenu : manageRoleMenuList) {
                baseService.delObject(manageRoleMenu);
            }
        }
        saveManageRoleMenu(roleVo.getMenuid(), manageRole.getId());
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getRoleBean(manageRole);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "del")
    public Object del(@RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            ManageRole manageRole = baseService.findById(ManageRole.class, id);
            state = baseService.delObject(manageRole);
            List<ManageResourceRole> manageRoleMenuList = getManageRoleMenu(manageRole.getId());
            if (manageRoleMenuList != null && manageRoleMenuList.size() > 0) {
                for (ManageResourceRole manageRoleMenu : manageRoleMenuList) {
                    baseService.delObject(manageRoleMenu);
                }
            }
            List<ManageRoleUser> manageUserRoleList = getManageUserRole(manageRole.getId());
            if (manageUserRoleList != null && manageUserRoleList.size() > 0) {
                for (ManageRoleUser manageUserRole : manageUserRoleList) {
                    baseService.delObject(manageUserRole);
                }
            }
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


}
